package sanrenxing.dayEnd.backRepertoryTotal.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sanrenxing.dayEnd.backRepertoryTotal.dao.EarningCountDao;
import sanrenxing.dayEnd.backRepertoryTotal.domain.BoundCountExtrat;
import sanrenxing.dayEnd.backRepertoryTotal.domain.CashCountExtrat;
import sanrenxing.dayEnd.backRepertoryTotal.domain.ManagerMoney;
import sanrenxing.dayEnd.backRepertoryTotal.service.EarningCountService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能: 收益计提
 * 作者：邓志强
 */
@RestController
@RequestMapping("/earningCount")
public class EarningCountController {

    //业务层对象
    @Autowired
    private EarningCountService earningCountService;

    /**
     * dao对象
     */
    @Autowired
    private EarningCountDao earningCountDao;
    /**
     * 现金提计的方法
     * @return
     */
    @GetMapping("/method")
    public Map<String,Object> query(HttpServletRequest request, int limit , int page){
        HttpSession session = request.getSession();
        Map<String, Object> map = new HashMap<String, Object>();
        List<CashCountExtrat> extrats = earningCountService.queryCashCount(((page-1)*limit)+1,(((page-1)*limit)+limit)+1);
        //判断是否已经统计：已统计
        if (session.getAttribute("EarningDate")!=null &&
                session.getAttribute("EarningDate").equals(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))){
            //遍历修改状态为已统计
            for (CashCountExtrat extrat : extrats) {
                extrat.setStates(1);
            }
        }else{
            //遍历修改状态为未统计
            for (CashCountExtrat extrat : extrats) {
                extrat.setStates(0);
            }
        }
        map.put("data",extrats);
        map.put("msg","");
        map.put("code",0);
        map.put("count",earningCountService.CashCount());

        return map;
    }

    /**
     * 债券计提的方法
     * @return
     */
    @GetMapping("/boundMethod")
    public Map<String,Object> boundQuery(HttpServletRequest request, int limit , int page){
        HttpSession session = request.getSession();
        Map<String,Object> map=new HashMap<>();
        List<BoundCountExtrat> extrats = earningCountService.queryBound(((page-1)*limit)+1,(((page-1)*limit)+limit)+1);
        //判断是否已经统计：已统计
        if (session.getAttribute("EarningDate")!=null &&
                session.getAttribute("EarningDate").equals(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))){
            //遍历修改状态为已统计
            for (BoundCountExtrat extrat : extrats) {
                extrat.setStates(1);
            }
        }else{
            //遍历修改状态为未统计
            for (BoundCountExtrat extrat : extrats) {
                extrat.setStates(0);
            }
        }
        map.put("code",0);
        map.put("msg","");
        map.put("count",earningCountService.bondCount());
        map.put("data",extrats);
        return map;
    }

    /**
     * 收益计提两费显示
     * @return
     */
    @GetMapping("/managerMethod")
    public Map<String,Object> ManagerQuery(HttpServletRequest request){
        HttpSession session = request.getSession();
        Map<String,Object> map=new HashMap<>();
        List<ManagerMoney> extrats = earningCountService.ManagerQeury();
        //判断是否已经统计：已统计
        if (session.getAttribute("EarningDate")!=null &&
                session.getAttribute("EarningDate").equals(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))){
            //遍历修改状态为已统计
            for (ManagerMoney extrat : extrats) {
                extrat.setStates(1);
            }
        }else{
            //遍历修改状态为未统计
            for (ManagerMoney extrat : extrats) {
                extrat.setStates(0);
            }
        }
        map.put("code",0);
        map.put("count",2);
        map.put("msg","");
        map.put("data",extrats);
        map.put("cashAccount",earningCountDao.queryCashAccount());
        return map;
    }

    /**
     * 收益计提点击统计按钮后添加应收应付
     * @return
     */
    @GetMapping("/total")
    public String total(HttpServletRequest request){
        HttpSession session = request.getSession();
        //判断当天已估值
        if (session.getAttribute("EarningDate")!=null &&
            session.getAttribute("EarningDate").equals(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))){
            return "already";
        }else{
            session.setAttribute("EarningDate",new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            return earningCountService.Alipay()?"success":"fail";
        }
        //return  earningCountService.Alipay()?"success":"fail";
    }


}
